/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * This code is based on the work of Paul Szulc.
 * 
 * Please check http://paulszulc.wordpress.com/2009/08/05/enhanced-wickettester/
 */
package com.googlecode.armycreator.enhancedwickettester;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;

public class EnhancedWicketTester {

    protected WicketTester tester;

    public EnhancedWicketTester(WicketTester tester) {
        this.tester = tester;
    }

    public WebRequestCycle setupRequestAndResponse() {
        return tester.setupRequestAndResponse();
    }

    public void assertComponentOnAjaxResponse(Component component) {
        tester.assertComponentOnAjaxResponse(component);
    }

    public void assertModelValue(String path, Object object) {
        tester.assertModelValue(path, object);
    }

    public Component getComponentFromLastRenderedPage(String path) {
        return tester.getComponentFromLastRenderedPage(path);
    }

    public void assertErrorMessages(String[] expectedErrorMessages) {
        tester.assertErrorMessages(expectedErrorMessages);
    }

    public void clickLink(String path) {
        tester.assertComponent(path, Link.class);
        tester.clickLink(path);
    }

    public void clickAjaxLink(String path) {
        tester.assertComponent(path, Link.class);
        tester.clickLink(path, true);
    }

    public void clickSubmitLink(String formId, String linkPath) {
        tester.assertComponent(formId + ":" + linkPath, SubmitLink.class);
        FormTester formTester = tester.newFormTester(formId);
        formTester.submit(linkPath);
    }

    public EnhancedFormTester form(String path) {
        return new EnhancedFormTester(tester, path);
    }

    public void assertEnabled(String path) {
        Component component = tester.getLastRenderedPage().get(path);
        assertTrue(component.isEnabled());
    }

    public void assertDisabled(String path) {
        Component component = tester.getLastRenderedPage().get(path);
        assertNotNull(component);
        assertFalse(component.isEnabled());
    }

    public Page startPage(Page page) {
        return tester.startPage(page);
    }

    public <C extends Page> void assertRenderedPage(Class<C> pageClass) {
        tester.assertRenderedPage(pageClass);
    }

    public WebSession getWicketSession() {
        return tester.getWicketSession();
    }
}
